﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace LightWadf.Filter
{
    /// <summary>
    /// <para>该过滤器将会把处理方法的结果进行缓存</para>
    /// <para>当收到请求时，如果存在缓存，会用缓存数据直接返回</para>
    /// <para>默认滑动缓存 5 分钟</para>
    /// <para>该过滤器适合结果不会随着 <see cref="System.Web.HttpContext.Request.Path"/> 的改变而变化的处理方法</para>
    /// </summary>
    public class CacheFilter : IFilter
    {
        private CacheItemPolicy policy = null;
        private static ObjectCache Cache = MemoryCache.Default;

        /// <summary>
        /// 默认滑动缓存 5 分钟
        /// </summary>
        public CacheFilter() : this(new TimeSpan(0, 5, 0))
        {
        }

        /// <summary>
        /// 用绝对过期时间初始化
        /// </summary>
        /// <param name="offset"></param>
        public CacheFilter(DateTimeOffset offset)
        {
            this.policy.AbsoluteExpiration = offset;
        }

        /// <summary>
        /// 用滑动过期时间初始化
        /// </summary>
        /// <param name="time"></param>
        public CacheFilter(TimeSpan time)
        {
            this.policy = new CacheItemPolicy();
            this.policy.SlidingExpiration = time;
        }

        public override object BeforeProcess(System.Web.HttpContext context)
        {
            string key = context.Request.Path;
            if (Cache.Contains(key))
            {
                return Cache.Get(key);
            }
            return null;
        }

        public override void AfterProcess(System.Web.HttpContext context, ref object result)
        {
            string key = context.Request.Path;
            if(!Cache.Contains(key))
                Cache.Set(key, result, this.policy);
        }
    }
}
